import config from '@/common/config.js'
const pop = {


	showToastTxt(msg, state = 1, url) {
		if (state == 1) {
			uni.showToast({
				title: msg,
				icon: 'none',
			})
		} else if (state == 2) {
			uni.showToast({
				title: msg,
				mask: true,
				icon: 'none',
				success: () => {
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 800)

				}
			})
		} else if (state == 3) {
			uni.showToast({
				title: msg,
				mask: true,
				icon: 'none',
				success: () => {
					setTimeout(() => {
						uni.redirectTo({
							url: url
						})
					}, 800)

				}
			})
		} else if (state == 4) {
			uni.showToast({
				title: msg,
				icon: 'none',
				mask: true,
				duration: 2000
			})
		}else if (state == 5) {
			uni.showToast({
				title: msg,
				icon: 'none',
				mask: true,
				duration: 3000
			})
		}
	},




	showLoading(msg) {
		uni.showLoading({
			title: msg,
			mask: true,
		})
	},
	hideLoading() {
		uni.hideLoading();
	},

	model(title, content = '') {
		return new Promise((resolve, reject) => {
			uni.showModal({
				title,
				content,
				cancelColor: '#cccccc',
				confirmColor: '#FF467F',
				success(res) {
					resolve(res.confirm)
				},
				fail(err) {
					reject(err)
				}
			})
		})
	},

	//  自定义跳转路径
	PageUrl(type, id) {
		if (type == 4) { // 文章列表
			uni.navigateTo({
				url: `/pages/JouList/JouList?listId=${id}`
			})
		}else if(type == 5){  // 文章详情
			uni.navigateTo({
				url: `/pages/Joudetails/Joudetails?article_id=${id}`
			})
		}else if(type == 2){ // 商品详情
			uni.navigateTo({
				url: `/pages/details/details?id=${id}`
			})
		}else if(type == 14){ // 视频分类
			uni.navigateTo({
				url: `/pages/classify/classify?class_id=${id}`
			})
		}
	},


	imgUrls(imgs) {
		const reg = /^(http|https)/;
		if(!imgs) return;
		if(imgs.search(reg) != -1) return imgs
		return config.imgUrl + imgs
	},

	navBack() {
		setTimeout(() => {
			uni.navigateBack({
				delta: 1
			})
		}, 600)
	},


	getViewInfo(selector) {
		return new Promise((resolve) => {
			const query = wx.createSelectorQuery()
			query.select(selector).boundingClientRect()
			query.exec((res) => {
				resolve(res)
			})
		})
	},
	

	
	pageRela(url){
		uni.reLaunch({
			url
		})
	},
	
	tabbar(){
		uni.reLaunch({
			url:'/pages/tabbar/tabbar'
		})
	},
	setClip(text){
		if(!text) return;
		uni.setClipboardData({
			data:text,
			success(){
				pop.showToastTxt('复制成功')
			},
			fail(){
			    pop.showToastTxt('复制失败')
			}
		})
	},
	
	relaunchTab(){
		uni.reLaunch({
			url: '/pages/tabbar/tabbar'
		})
	}
	
}


export default pop
